<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        .container { width: 300px; margin: 100px auto; }
        input { margin: 5px 0; width: 100%; }
    </style>
</head>
<body>
<div class="container">
    <h2>用户登录</h2>
    <input type="text" id="number" placeholder="账号"><br>
    <input type="password" id="password" placeholder="密码"><br>
    <button onclick="login()">登录</button>
    <button onclick="location.href='register.html'">注册</button>
    <p id="message"></p>
</div>

<script>
    async function login() {
        const account = {
            number: document.getElementById('number').value,
            password: document.getElementById('password').value
        };

        try {
            const response = await fetch('/api/account/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(account)
            });

            if (!response.ok) {
                throw new Error(`HTTP 错误! 状态码: ${response.status}`);
            }

            const result = await response.json();
            if (result.success) {
                // 存储用户信息
                sessionStorage.setItem('currentUser', account.number);
                document.getElementById('message').textContent = "登录成功！正在跳转...";
                setTimeout(() => {
                    window.location.href = 'homepage.html';
                }, 1500);
            } else {
                document.getElementById('message').textContent = "账号或密码错误！";
            }
        } catch (error) {
            document.getElementById('message').textContent = "登录失败: " + error.message;
        }
    }
</script>
</body>
</html>